package net.googlecode.morenko.task4;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;

/**
 * Class with methods that realize task4.
 */
public class StringNormalization {
    /**
     * Constant that represent the sentence.
     */
    private final String STR = "This   IS a test \n  sentence.";

    /**
     * Method that delete unnecessary delimiters and makes
     * each word in correct register.
     * @return correct sentence.
     */
    public String normalize() {
        StringTokenizer stringTokenizer = new StringTokenizer(STR, " \n\t");
        StringBuffer stringBuffer = new StringBuffer(STR.length());
        while (stringTokenizer.hasMoreTokens()) {
            stringBuffer.append(stringTokenizer.nextToken()).append(" ");
        }
        if (Character.isLowerCase(stringBuffer.charAt(0))) {
            stringBuffer.setCharAt(0, stringBuffer.charAt(0));
        }
        for (int j = 1; j < stringBuffer.length(); j++) {
            stringBuffer.setCharAt(j, Character.toLowerCase(stringBuffer.charAt(j)));
        }
        return stringBuffer.toString();
    }

    /**
     * Method that makes first letter of each word in upper case.
     * @return modified sentence.
     */
    public String wordToUpperCase() {
        StringTokenizer stringTokenizer = new StringTokenizer(STR, " ,.\n\t;:-", true);
        StringBuffer stringBuffer = new StringBuffer(STR.length());
        while (stringTokenizer.hasMoreTokens()) {
            StringBuffer tmp = new StringBuffer(stringTokenizer.nextToken());
            tmp.setCharAt(0, Character.toUpperCase(tmp.charAt(0)));
            stringBuffer.append(tmp);
        }
        return stringBuffer.toString();
    }

    /**
     * Method that reverse each word of the sentence.
     * @return modified sentence.
     */
    public String reverse() {
        StringTokenizer stringTokenizer = new StringTokenizer(STR, " ,.\n\t;:-", true);
        StringBuffer stringBuffer = new StringBuffer(STR.length());
        while (stringTokenizer.hasMoreTokens()) {
            StringBuffer tmp = new StringBuffer(stringTokenizer.nextToken());
            stringBuffer.append(tmp.reverse());
        }
        return stringBuffer.toString();
    }

    /**
     * Method that replace each letter 'A' or 'a' to '!'.
     * @return modified sentence.
     */
    public String replace() {
        return STR.replace("A", "!").replace("a", "!");
    }

    /**
     * Method that add 20 day earlier date at the end of the sentence in format "dd-MM-yyyy hh:mm".
     * @param delta variable that indicates date offset.
     * @return modified sentence.
     */
    public String addDate(int delta) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm");
        calendar.add(Calendar.DAY_OF_MONTH, delta);
        return new StringBuffer(STR).append(format.format(calendar.getTime())).toString();
    }
}
